package cn.itsource.ymcc.web.controller;

import cn.itsource.openfeign.EsCourseFeignClient;
import cn.itsource.ymcc.common.result.JsonResult;
import cn.itsource.ymcc.common.result.PageList;
import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.domain.CourseDoc;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.dto.AddCourseDto;
import cn.itsource.ymcc.query.CourseQuery;
import cn.itsource.ymcc.service.ICourseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    @Autowired
    private EsCourseFeignClient esCourseFeignClient;

    @PostMapping("/info")
    public JsonResult info(@RequestBody List<Long> ids){
        JsonResult jsonResult = esCourseFeignClient.getCourseDocByCourseIds(ids);
        Object data = jsonResult.getData();
        List<CourseDoc> courseDocList = JSONObject.parseArray(JSON.toJSONString(data), CourseDoc.class);
        return JsonResult.success().setData(courseDocList);
    }

    @GetMapping("/detail/data/{courseId}")
    public JsonResult getDetailData(@PathVariable("courseId")Long courseId){
        return courseService.getDetailData(courseId);
    }


    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Long> ids){
        return courseService.onLineCourse(ids);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody @Valid AddCourseDto courseDto, HttpServletRequest request){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        Login login = JSONObject.parseObject(name, Login.class);
        System.out.println(login.getId());
        System.out.println(login.getUsername());
//        String header = request.getHeader(Constants.TOKEN);
//        Object currentUser = JwtUtils.getCurrentUser(header);
//        Long currentId;
//        String username;
//        Login login = JSON.parseObject(currentUser.toString(), Login.class);
//        if(currentUser instanceof Login){
//            currentId =  ((Login) currentUser).getId();
//            username =  ((Login) currentUser).getUsername();
//        }else {
//            currentId =  ((User) currentUser).getId();
//            username = ((User) currentUser).getNickName();
//        }

//        AssertUtil.isNotNull(courseDto.getCourse(),"课程不能为空");
//        if(courseDto.getCourse().getId()!=null){
//            courseService.updateById(courseDto,login.getId(),login.getUsername());
//        }else{
//            courseService.save(courseDto,login.getId(),login.getUsername());
//        }
        return JsonResult.success();
    }

    @GetMapping("/courseInfo/{id}")
    public JsonResult courseInfo(@PathVariable("id")Long id){
        return courseService.courseInfo(id);
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(query.getKeyword()),Course::getName,query.getKeyword());
        queryWrapper.orderByDesc(Course::getId);
        page = courseService.page(page,queryWrapper);
        return JsonResult.success().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
